package com.monkeysonnet.b2dFluent;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Joint;
import com.badlogic.gdx.physics.box2d.JointDef;
import com.badlogic.gdx.physics.box2d.World;

public abstract class JointFactory<T extends JointFactory<?>>
{
	private JointDef _def;
	private boolean _managed;
	
	protected JointFactory(boolean managed)
	{
		_managed = managed;
	}
	
	protected void setDef(JointDef def)
	{
		_def = def;
	}
	
	protected void clear(JointDef initialDef)
	{
		_def.bodyA = initialDef.bodyA;
		_def.bodyB = initialDef.bodyB;
		_def.collideConnected = initialDef.collideConnected;
	}
	
	@SuppressWarnings("unchecked")
	public T bodyA(Body b)
	{
		_def.bodyA = b;
		return (T)this;
	}
	
	@SuppressWarnings("unchecked")
	public T bodyB(Body b)
	{
		_def.bodyB = b;
		return (T)this;
	}
	
	@SuppressWarnings("unchecked")
	public T between(Body a, Body b)
	{
		_def.bodyA = a;
		_def.bodyB = b;
		return (T)this;
	}
	
	public Joint create(World w)
	{
		if(_managed)
			free();
		return w.createJoint(_def);
	}
	
	public abstract void free();
}
